package com.ruoyi.stuactivity.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.stuactivity.domain.StudentActivityInfo;
import com.ruoyi.stuactivity.mapper.StudentActivityInfoMapper;
import com.ruoyi.stuactivity.service.IStudentAbilityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ruoyi.framework.datasource.DynamicDataSourceContextHolder.log;


@Service
public class StudentAbilityServiceImpl implements IStudentAbilityService {

    @Autowired
    private StudentActivityInfoMapper activityMapper;


    /**
     *根据学生ID查询文体活动
     */
    @Override
    public List<StudentActivityInfo> listwentiActivities(String studentId) {
        return activityMapper.selectwentiActivitiesByStudentId(studentId);
    }

    @Override
    public double calculatewentiScore(String studentId) {
        Map<String, Double> weights = new HashMap<>();
        weights.put("院级", 0.1);
        weights.put("校级", 0.2);
        weights.put("省级", 0.3);
        weights.put("国赛", 0.4);

        List<StudentActivityInfo> wtactivity = listwentiActivities(studentId);
        double wtscore = 0;
        for (StudentActivityInfo activity : wtactivity) {
            // 检查comprehensiveScore是否为null
            Double comprehensiveScore = activity.getComprehensiveScore();
            if (comprehensiveScore == null) {
                log.warn("学生ID={}的活动{}综测分为null，跳过计算",
                        studentId, activity.getActivityName());
                continue;
            }
            Double weight = weights.getOrDefault(activity.getActivityLevel(), 0.0);
            wtscore += 100 * weight * comprehensiveScore;
        }

        return Math.max(0, Math.min(100, wtscore));
    }

    @Override
    public double calculateshijianScore(String studentId) {
        // 1. 查询违规次数
        int violationCount = activityMapper.selectshijianActivityByStudentId(studentId);

        // 2. 根据违规次数计算分数
        double adjustedScore;
        switch (violationCount) {
            case 0:
                adjustedScore = 100; // 无违规，满分
                break;
            case 1:
                adjustedScore = 90;  // 1次违规，90分
                break;
            case 2:
                adjustedScore = 70;   // 2次违规，70分
                break;
            case 3:
                adjustedScore = 60;  // 3次违规，60分
                break;
            default:
                adjustedScore = 50;  // 超过3次违规，50分
        }

        // 确保分数在 0-100 范围内（虽然当前逻辑不会超出，但保险起见）
        return Math.max(0, Math.min(100, adjustedScore));
    }

    @Override
    public double calculatesixiangScore(String studentId) {
        Map<String, Double> weights = new HashMap<>();
        weights.put("院级", 0.1);
        weights.put("校级", 0.2);
        weights.put("省级", 0.3);
        weights.put("国赛", 0.4);

        List<StudentActivityInfo> sxactivity = listsixiangActivityByStudentId(studentId);
        double sxscore = 0;
        for (StudentActivityInfo activity : sxactivity) {
            // 检查comprehensiveScore是否为null
            Double comprehensiveScore = activity.getComprehensiveScore();
            if (comprehensiveScore == null) {
                log.warn("学生ID={}的活动{}综测分为null，跳过计算",
                        studentId, activity.getActivityName());
                continue;
            }
            Double weight = weights.getOrDefault(activity.getActivityLevel(), 0.0);
            sxscore += 100 * weight * comprehensiveScore;
        }

        return Math.max(0, Math.min(100, sxscore));
    }

    @Override
    public double calculatejntcScore(String studentId) {
        Map<String, Double> weights = new HashMap<>();
        weights.put("院级", 0.1);
        weights.put("校级", 0.2);
        weights.put("省级", 0.3);
        weights.put("国赛", 0.4);

        List<StudentActivityInfo> jnactivity = listjntcActivityByStudentId(studentId);
        double jnscore = 0;
        for (StudentActivityInfo activity : jnactivity) {
            // 检查comprehensiveScore是否为null
            Double comprehensiveScore = activity.getComprehensiveScore();
            if (comprehensiveScore == null) {
                log.warn("学生ID={}的活动{}综测分为null，跳过计算",
                        studentId, activity.getActivityName());
                continue;
            }
            Double weight = weights.getOrDefault(activity.getActivityLevel(), 0.0);
            jnscore += 100 * weight * comprehensiveScore;
        }

        return Math.max(0, Math.min(100, jnscore));
    }

    @Override
    public List<StudentActivityInfo> listsixiangActivityByStudentId(String studentId) {
        return activityMapper.selectsixiangActivityByStudentId(studentId);
    }


    /**
     * 根据学生ID查询创新创业活动
     */
    @Override
    public List<StudentActivityInfo> listInnovationActivities(String studentId) {
        return activityMapper.selectInnovationActivitiesByStudentId(studentId);
    }

    @Override
    public List<StudentActivityInfo> listjntcActivityByStudentId(String studentId) {
        return activityMapper.selectjntcActivityByStudentId(studentId);
    }

    /**
     * 计算学生创新思维得分
     * 规则：院级(0.1)、校级(0.2)、省级(0.3)、国赛(0.4)，基于综测分计算
     */
    @Override
    public double calculateInnovationScore(String studentId) {
        Map<String, Double> weights = new HashMap<>();
        weights.put("院级", 0.1);
        weights.put("校级", 0.2);
        weights.put("省级", 0.3);
        weights.put("国赛", 0.4);

        List<StudentActivityInfo> activities = listInnovationActivities(studentId);
        double score = 0;
        for (StudentActivityInfo activity : activities) {
            // 检查comprehensiveScore是否为null
            Double comprehensiveScore = activity.getComprehensiveScore();
            if (comprehensiveScore == null) {
                log.warn("学生ID={}的活动{}综测分为null，跳过计算",
                        studentId, activity.getActivityName());
                continue;
            }
            Double weight = weights.getOrDefault(activity.getActivityLevel(), 0.0);
            score += 100 * weight * comprehensiveScore;
        }

        return Math.max(0, Math.min(100, score));

    }

    @Override
    public double calculatezhishiScore(String studentId) {
        return activityMapper.selectzhishiByStudentId(studentId);
    }


    /**
     * 获取学生能力综合数据
     */
    @Override
    public AjaxResult getStudentAbilityData(String studentId) {
        // 1. 计算创新思维得分和文体得分
        double innovationScore = calculateInnovationScore(studentId);
        double wentiScore = calculatewentiScore(studentId);
        double shijianScore = calculateshijianScore(studentId);
        double jntcScore = calculatejntcScore(studentId);
        double sixiangScore = calculatesixiangScore(studentId);


        Map<String, Double> abilityScores = new HashMap<>();
        abilityScores.put("knowledgeMastery", 85.0);     // 知识掌握
        abilityScores.put("problemSolving", jntcScore);       // 问题解决
        abilityScores.put("teamwork", wentiScore);       // 团队协作
        abilityScores.put("communication", shijianScore);        // 沟通表达
        abilityScores.put("selfLearning", sixiangScore);         // 自主学习
        abilityScores.put("creativity", innovationScore); // 创新思维

        // 3. 返回数据
        return AjaxResult.success(abilityScores);
    }
}
